//index.js
const util = require('../../utils/util.js')

Page({
  data: {
    advert_list: [{
        id: 1,
        img: "https://app.svell.cn/test.png",
        type: "usemap",
        area: [{
            shape: "rect",
            coords: "100,85,220,190",
            type: "path",
            path: "../page_a/page_a"
          },
          {
            shape: "rect",
            coords: "470,95,645,220",
            type: "path",
            path: "../page_b/page_b"
          },
          {
            shape: "rect",
            coords: "58,380,240,510",
            type: "path",
            path: "../page_c/page_c"
          },
          {
            shape: "rect",
            coords: "485,396,645,515",
            type: "path",
            path: "../page_d/page_d"
          },
        ],
        ratio: 0
      },
      {
        id: 3,
        img: "https://www.svell.cn/ueditor/jsp/upload/image/20180112/1515747549437005541.png",
        type: "path",
        path: "../page_e/page_e",
        ratio: 0
      }
    ]
  },
  advertBindload: function (e) {
    var _this = this;
    let windowWidth = wx.getSystemInfoSync().windowWidth;
    let advertWidth = e.detail.width;
    let ratio = windowWidth / advertWidth;
    _this.data.advert_list[e.target.dataset.index].ratio = ratio
  },
  advertBindtap: function (e) {
    var _this = this;
    let advert = _this.data.advert_list[e.target.dataset.index]
    if (advert.type == "usemap") {
      let area = util.getAdvertArea(e.detail, advert.area, advert.ratio)
      if (area) {
        console.log(area)
        wx.redirectTo({
          url: area.path
        })
      }
    } else if (advert.type == "path") {
      console.log(advert)
      wx.redirectTo({
        url: advert.path
      })
    }
  }
})